#include "Thread.h"

int Thread::Start()
{
    pthread_attr_t attr;
    pthread_attr_init(&attr);
    pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
    //if (m_bDetach)
    //    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
    int ret = pthread_create(&m_tid, &attr, ThreadFunc, (void *)this);
    pthread_attr_destroy(&attr);
    return ret;
}

//int Thread::Join(pthread_t tid)
//{
//    return pthread_join(tid, NULL);
//}
//
//int Thread::Cancel(pthread_t tid)
//{
//    return pthread_cancel(tid);
//}

int Thread::Join()
{
    return pthread_join(m_tid, NULL);
}

int Thread::Cancel()
{
    return pthread_cancel(m_tid);
}

pthread_t Thread::Self()
{
    //return pthread_self();
    return m_tid;
}

void Thread::Terminate()
{
    pthread_exit(PTHREAD_CANCELED);
}

bool Thread::IsEqual(pthread_t tid)
{
    if (pthread_equal(m_tid, tid) == 0)
        return false;
    else
        return true;
}

bool Thread::IsDetach()
{
    return m_bDetach;
}

void* Thread::ThreadFunc(void* ptr)
{
    ((Thread *)ptr)->Run();
    return ptr;
}

void Thread::SetThreadPool(ThreadPool* pThreadPool)
{
    m_pThreadPool = pThreadPool;
}

void Thread::SetThreadWaiter(ThreadWaiter* pThreadWaiter)
{
    m_pThreadWaiter = pThreadWaiter;
}

